// {{{ tags () - Tags input box
liwe.form5.templates [ 'tags-list' ] = '<div class="tags-list" id="%(_tagid)s">%(_items)s</div><datalist id="%(_datalist)s"></datalist>';
liwe.form5.templates [ 'tags-del' ] = '<div class="tag-del-cnt"><div class="tag-del" onclick="liwe.form5._tags_del(\'%(form)s\',\'%(name)s\',\'%(tag)s\')"></div>%(tag)s</div>';

liwe.form5.plugin_add ( 'tags', function ( frm, type, name, attrs )
{
	if ( ! attrs [ '_tags' ] ) attrs [ '_tags' ] = list ();

	attrs [ '_tagid' ] = frm [ 'name' ] + '-' + name + '-tags';
	attrs [ 'list' ] = frm [ 'name' ] + '-' + name + '-tagslist';
	attrs [ 'filter' ] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz 0123456789_-,";
	attrs [ 'onblur' ] = String.formatDict ( "liwe.form5._tags_refresh('%(form)s','%(name)s')", { form: frm.name, name: name } );
	attrs [ 'onkeydown' ] = String.formatDict ( "liwe.form5._tags_keydown(event,'%(form)s','%(name)s')", { form: frm.name, name: name } );
	attrs [ 'onkeyup' ] = String.formatDict ( "liwe.form5._tags_datalist('%(form)s','%(name)s')", { form: frm.name, name: name } );
	attrs [ 'get_value' ] = function ()
	{
		return attrs [ '_tags' ].join ( '|' );
	};
	attrs [ 'set_value' ] = function ( v )
	{
		attrs [ '_tags' ] = list ( v.split ( '|' ) );
		liwe.form5._tags_refresh ( frm.name, name );
	};
	attrs [ 'pre-render' ] = function ( form, item )
	{
		item [ 'type' ] = 'text';
		if ( item [ 'value' ] )
		{
			item [ '_tags' ] = list ( item [ 'value' ].split ( '|' ) );
			delete item [ 'value' ];
		}
		tags.list ( item [ 'module' ], function ( res )
		{
			item [ '_alltags' ] = res.keys ();
		} );
	};
	attrs [ 'render' ] = function ( form, item )
	{
		var s = new String.buffer ();
		s.add ( String.formatDict ( form.templates [ 'input' ], item ) );
		var dct = {
			'_tagid': item [ '_tagid' ],
			'_items': liwe.form5._tags_to_items ( form.name, name, item [ '_tags' ] ),
			'_datalist': item [ 'list' ]
		};
		s.add ( String.formatDict ( form.templates [ 'tags-list' ], dct ) );
		return s.toString ();
	};
} );

liwe.form5._tags_refresh = function ( form_name, item_name )
{
	var form = liwe.form5.get ( form_name );
	var item = form.get_item_by_name ( item_name );

	var tags_text = $v ( item [ '_id' ] );
	var taglist = item [ '_tags' ];

	var tags = list ( tags_text.split ( "," ) );
	tags.iterate ( function ( t )
	{
		t = t.strip ();
		if ( ! t ) return;
		if ( taglist.indexOf ( t ) >= 0 ) return;
		taglist.push ( t );
	} );

	var s = liwe.form5._tags_to_items ( form_name, item_name, taglist );

	$( item [ '_tagid' ], s );

	$( item [ '_id' ] ).value = '';
};

liwe.form5._tags_to_items = function ( form_name, item_name, taglist )
{
	var form = liwe.form5.get ( form_name );
	var s = new String.buffer ();
	taglist.iterate ( function ( t )
	{
		t = t.trim ();
		if ( ! t ) return;
		s.add ( String.formatDict ( form.templates [ 'tags-del' ], { form: form_name, name: item_name, tag: t } ) );
	} );

	return s.toString ();
};

liwe.form5._tags_del = function ( form_name, item_name, tag )
{
	var form = liwe.form5.get ( form_name );
	var item = form.get_item_by_name ( item_name );
	var taglist = item [ '_tags' ];
	var idx = taglist.indexOf ( tag );

	if ( idx < 0 ) return;

	taglist.del ( idx );

	liwe.form5._tags_refresh ( form_name, item_name );
};

liwe.form5._tags_datalist = function ( form_name, item_name )
{
	var form = liwe.form5.get ( form_name );
	var item = form.get_item_by_name ( item_name );

	if ( ! item [ '_alltags' ] ) return;

	var dl = $( item [ 'list' ] );
	if ( dl.childNodes.length ) return;
	var s = new String.buffer ();
	item [ '_alltags' ].iterate ( function ( t )
	{
		s.add ( '<option value="' + t + '" />' );
	} );
	dl.innerHTML = s.toString ();
};

liwe.form5._tags_keydown = function ( e, form_name, item_name )
{
	switch ( e.keyCode )
	{
		case 9:
			if ( e.target.value.length == 0 ) return;

		case 13:
		case 188:
			e.preventDefault ();

			setTimeout ( function () { liwe.form5._tags_refresh ( form_name, item_name ) }, 50 );
			return true;
	}
};
// }}}
